package user

import (
	"fmt"
	"gitee.com/mootok/mootok/src/constant/config"
	redis2 "gitee.com/mootok/mootok/src/constant/strings/redis"
	"gitee.com/mootok/mootok/src/storage/redis"
	"github.com/gin-gonic/gin"
)

var userLoginTokenPrefix = config.EnvConfig.RedisPrefix + "user_login_token:"

type Service struct {
}

func genUserLoginTokenKey(cookie string) string {
	return fmt.Sprintf("%s%s", userLoginTokenPrefix, cookie)
}

func (*Service) GetUserIdByToken(c *gin.Context, cookie string) (string, error) {
	redisKey := genUserLoginTokenKey(cookie)
	value, err := redis.Client.Get(c, redisKey).Result()
	return value, err
}

func (*Service) ReNewUserLoginExpiredTime(c *gin.Context, cookie string) error {
	redisKey := genUserLoginTokenKey(cookie)
	err := redis.Client.Expire(c, redisKey, redis2.UserLoginRedisExpireTime).Err()
	return err
}

func (*Service) SetUserIdByToken(c *gin.Context, token string, userId int) error {
	redisKey := genUserLoginTokenKey(token)
	err := redis.Client.Set(c, redisKey, userId, redis2.UserLoginRedisExpireTime).Err()
	return err
}

func (*Service) RemoveUserIdByToken(c *gin.Context, cookie string) error {
	redisKey := genUserLoginTokenKey(cookie)
	err := redis.Client.Del(c, redisKey).Err()
	return err
}
